package com.api.gbhg.liuwenbo.api;


import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.dao.ProjectDetailsUtil;
import com.api.gbhg.liuwenbo.pojo.Info;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.liuwenbo.utils.Utils;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetData;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 * 项目台账(查看详情)
 */
@Path("/gb/workflowDetails")
public class ProjectDetailsApi {

    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    private static final Logger LOGGER = LoggerFactory.getLogger("gbhg");

    @POST
    @Path("/getDetailsData")
    @Produces(MediaType.APPLICATION_JSON)
    public String DetailsData(Map<String,Object> param){

        if (!param.containsKey("requestId")){
            Map<String,Object> map  = new HashMap<>();
            map.put("retcode","-1");
            map.put("retmsg","请检查上送参数！为必填项");
            return JSON.toJSONString(map);
        }

        Info[] infos = getSysDataById(param.get("requestId").toString());
        return JSON.toJSONString(infos);
    }

    /**
     * 根据requestId获取流程数据详情
     * @param requestId
     * @return
     */
    public Info[] getSysDataById(String requestId){
        RecordSet rs = new RecordSet();

        String lcid = ProjectDetailsUtil.getFlowIdByRequestId(requestId);
        String title = ProjectDetailsUtil.getFlowIdByRequestName(requestId);
        String flowTableName = VersionFlowUtil.getFlowTableName(lcid);

        //获取主表数据
        Map<String, Object> tabData = getMainTabData(flowTableName, requestId);

        int mainTableId = VersionFlowUtil.getFlowTableLableId(lcid);
        //获取明细表名称
        List<String> flowDetailTableNames = VersionFlowUtil.getDetailTables(mainTableId);

        Info[] info = new Info[flowDetailTableNames.size()+1];
        Map<String,Object> map = new HashMap<>();
        info[0] = new Info();
        map.putAll(tabData);
        map.put("title",title);
        info[0].setTitle("tab");
        info[0].setTable(map);


        rs.execute("select id from "+flowTableName+" where requestId="+requestId);
        rs.next();
        int mainId= rs.getInt("id");

        //获取明细数据
        for (int i = 0; i < flowDetailTableNames.size(); i++) {

            Map<String, Object>[] maps = getdtTable2(flowDetailTableNames.get(i), mainId,requestId);
            if (maps.length>0){
                info[i+1] = new Info();
                info[i+1].setData(Utils.removeExcessLen(maps));
                info[i+1].setTitle(flowDetailTableNames.get(i).substring(flowDetailTableNames.get(i).length()-3));
            }
        }
        return Utils.removeExcessLenInfo(info);
    }

    public static String getSyed(String requestId, int jjfl){
        RecordSet rs = new RecordSet();
        String sql = " select syed from uf_jingfei where requestid = "+requestId+" and bmzcjjfl="+jjfl;
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("syed");
    }

    /**
     * 获取明细表数据
     * @param dtName
     * @param mainId
     * @return
     */
    public Map<String,Object>[] getdtTable2(String dtName,int mainId,String requestId){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from "+dtName+" where mainid = "+mainId+" ");
        RecordSetData data = rs.getData();

        String[] columnName = data.getColumnName();
        int[] columnType = data.getColumnType();

        Map<String,Object>[] maps = new HashMap[rs.getCounts()];
        int t = 0;
        int count = 0;
        while (rs.next()){
            maps[t] = new HashMap<>();
            Map<String,Object> map = new HashMap<>();
            for (int i = 0; i < columnName.length; i++) {
                if(columnType[i] == 3){
                    map.put(columnName[i],FormatUtil.DmFormat(rs.getDouble(columnName[i])));
                }else if(columnName[i].equals("lx")  || columnName[i].equals("sblx") ||
                        columnName[i].equals("pxlb") || columnName[i].equals("ts") ||       //转显示值
                        columnName[i].equals("pxdd") || columnName[i].equals("sfzxys") ||
                        columnName[i].equals("wyxz") || columnName[i].equals("srlx")){
                    String boxVal = ProjectDetailsUtil.getSelectionBoxVal(dtName, rs.getInt(columnName[i]), columnName[i]);
                    map.put(columnName[i],boxVal);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].contains("dw") && !columnName[i].equals("jldw")){ //单位
                    //查申报单位
                    RecordSet sbdw = rmcd.getSBDWById(rs.getInt(columnName[i]));
                    sbdw.next();
                    map.put(columnName[i],sbdw.getString("SUBCOMPANYNAME"));
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].contains("debzmrmn")){ //定额标准(每人每年）
                    String debztz = ProjectDetailsUtil.getDebztz(rs.getInt(columnName[i]));
                    map.put(columnName[i],debztz);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].contains("dyzfjjfl") ){ //经济支出分类科目类-政府
                    String jjfl = ProjectDetailsUtil.getJjzcflkmk(rs.getInt(columnName[i]));
                    map.put(columnName[i],jjfl);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }
                else if( columnName[i].contains("bmyszcjjfl") ||
                        columnName[i].contains("bmjjyszcjjfl")){ //经济支出分类科目款-部门
                    String jjfl = ProjectDetailsUtil.getJjzcflkmk(rs.getInt(columnName[i]));
                    map.put(columnName[i],jjfl);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));

                    String syed = getSyed(requestId,rs.getInt(columnName[i]));
                    map.put("syed",syed);
                }else if(columnName[i].equals("jldw")){ //计量单位
                    String jldw = ProjectDetailsUtil.getLgcAssetUnit(rs.getInt(columnName[i]));
                    map.put(columnName[i],jldw);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].equals("ysly")){ //预算来源
                    String ysly = ProjectDetailsUtil.getBudgetSources(rs.getInt(columnName[i]));
                    map.put(columnName[i],ysly);
                }else if(columnName[i].contains("gnkm")){ //功能科目
                    String gnkm = ProjectDetailsUtil.getGnkm(rs.getInt(columnName[i]));
                    map.put(columnName[i],gnkm);
                }else if(columnName[i].equals("dfczbkxm")){ //地方财政拨款项目
                    String dfczbkxm = ProjectDetailsUtil.getDfczbkxm(rs.getInt(columnName[i]));
                    map.put(columnName[i],dfczbkxm);
                }else if(columnName[i].equals("zcqd")){
                    String zcqd = ProjectDetailsUtil.getFlName(rs.getInt(columnName[i]));
                    if (zcqd.isEmpty()){
                        continue;
                    }
                    map.put(columnName[i],zcqd);
                }else if(columnName[i].equals("zyczbksr")){
                    String mc = ProjectDetailsUtil.getZysrName(rs.getString(columnName[i]));
                    if (mc.isEmpty()){
                        continue;
                    }
                    map.put(columnName[i],mc);
                }else if(columnName[i].equals("dfczbkxmsnjz") || columnName[i].equals("dfczbkxmdnys")){
                    String xmmc = ProjectDetailsUtil.getFzyxmName(rs.getString(columnName[i]));
                    if (xmmc.isEmpty()){
                        continue;
                    }
                    map.put(columnName[i],xmmc);
                }else{
                    map.put(columnName[i],rs.getString(columnName[i]));
                }
            }
            maps[t].putAll(map);
            t++;
        }
        return maps;
    }


    /**
     * 获取明细表数据
     * @param dtName
     * @param mainId
     * @return
     */
    public Map<String,Object>[] getdtTable(String dtName,int mainId){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from "+dtName+" where mainid = "+mainId+" ");
        RecordSetData data = rs.getData();

        String[] columnName = data.getColumnName();
        int[] columnType = data.getColumnType();

        Map<String,Object>[] maps = new HashMap[rs.getCounts()];
        int t = 0;
        int count = 0;
        while (rs.next()){
            maps[t] = new HashMap<>();
            Map<String,Object> map = new HashMap<>();
            for (int i = 0; i < columnName.length; i++) {
                if(columnType[i] == 3){
                    map.put(columnName[i],FormatUtil.DmFormat(rs.getDouble(columnName[i])));
                }else if(columnName[i].equals("lx")  || columnName[i].equals("sblx") ||
                        columnName[i].equals("pxlb") || columnName[i].equals("ts") ||       //转显示值
                        columnName[i].equals("pxdd") || columnName[i].equals("sfzxys") ||
                        columnName[i].equals("wyxz") || columnName[i].equals("srlx")){
                    String boxVal = ProjectDetailsUtil.getSelectionBoxVal(dtName, rs.getInt(columnName[i]), columnName[i]);
                    map.put(columnName[i],boxVal);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].contains("dw") && !columnName[i].equals("jldw")){ //单位
                    //查申报单位
                    RecordSet sbdw = rmcd.getSBDWById(rs.getInt(columnName[i]));
                    sbdw.next();
                    map.put(columnName[i],sbdw.getString("SUBCOMPANYNAME"));
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].contains("debzmrmn")){ //定额标准(每人每年）
                    String debztz = ProjectDetailsUtil.getDebztz(rs.getInt(columnName[i]));
                    map.put(columnName[i],debztz);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].contains("dyzfjjfl") || columnName[i].contains("bmyszcjjfl") ||
                        columnName[i].contains("bmjjyszcjjfl")){ //经济支出分类科目款
                    String jjfl = ProjectDetailsUtil.getJjzcflkmk(rs.getInt(columnName[i]));
                    map.put(columnName[i],jjfl);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));


                }else if(columnName[i].equals("jldw")){ //计量单位
                    String jldw = ProjectDetailsUtil.getLgcAssetUnit(rs.getInt(columnName[i]));
                    map.put(columnName[i],jldw);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].equals("ysly")){ //预算来源
                    String ysly = ProjectDetailsUtil.getBudgetSources(rs.getInt(columnName[i]));
                    map.put(columnName[i],ysly);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].contains("gnkm")){ //功能科目
                    String gnkm = ProjectDetailsUtil.getGnkm(rs.getInt(columnName[i]));
                    map.put(columnName[i],gnkm);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].equals("dfczbkxm")){ //地方财政拨款项目
                    String dfczbkxm = ProjectDetailsUtil.getDfczbkxm(rs.getInt(columnName[i]));
                    map.put(columnName[i],dfczbkxm);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].equals("zcqd")){
                    String zcqd = ProjectDetailsUtil.getFlName(rs.getInt(columnName[i]));
                    if (zcqd.isEmpty()){
                        continue;
                    }
                    map.put(columnName[i],zcqd);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].equals("zyczbksr")){
                    String mc = ProjectDetailsUtil.getZysrName(rs.getString(columnName[i]));
                    if (mc.isEmpty()){
                        continue;
                    }
                    map.put(columnName[i],mc);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else if(columnName[i].equals("dfczbkxmsnjz") || columnName[i].equals("dfczbkxmdnys")){
                    String xmmc = ProjectDetailsUtil.getFzyxmName(rs.getString(columnName[i]));
                    if (xmmc.isEmpty()){
                        continue;
                    }
                    map.put(columnName[i],xmmc);
                    map.put(columnName[i]+"id",rs.getInt(columnName[i]));
                }else{
                    map.put(columnName[i],rs.getString(columnName[i]));
                }
            }
            maps[t].putAll(map);
            t++;
        }
        return maps;
    }

    /**
     * 获取表数据
     * @param flowTableName
     * @param requestId
     * @return
     */
    public Map<String,Object> getMainTabData(String flowTableName,String requestId){
        RecordSet rs = new RecordSet();
        String sql = "select * from "+flowTableName+" where requestId = "+requestId+" ";
        LOGGER.info("项目台账详情查询主表sql-------------："+sql+"");
        rs.executeQuery(sql);
        RecordSetData data = rs.getData();
        rs.next();
        Map<String,Object> map = new HashMap<>();

        String[] columnName = data.getColumnName();
        int[] columnType = data.getColumnType();
        for (int i = 0; i < columnName.length ; i++) {
            if(columnName[i].equals("fzr") || columnName[i].equals("jbr")){
                // 查负责人
                //编报人
                RecordSet jbr = rmcd.getUserNameById(rs.getInt(columnName[i]));
                jbr.next();
                String loginid = jbr.getString("loginid");
                if(loginid.equals("")){
                    loginid="系统管理员";
                }
                map.put(columnName[i],loginid);
            }else if(columnName[i].equals("sbdw") || columnName[i].equals("sydw") || columnName[i].equals("sydanwei")){
                //查申报单位   xjdwsjsrhpje   select * from formtable_main_655 where requestId = 482488
                LOGGER.info("项目台账详情查询单位-------------列明："+columnName[i]+" ------值："+rs.getString(columnName[i])+"");
                RecordSet sbdw = ProjectDetailsUtil.getSBDWById(rs.getString(columnName[i]));
                StringBuffer dwStr = new StringBuffer("");
                while (sbdw.next()){
                    dwStr.append(sbdw.getString("SUBCOMPANYNAME")+"  ");
                }
                map.put(columnName[i],dwStr);
            }else if(columnName[i].contains("sbks")){
                // 查申报科室
                RecordSet sbks = rmcd.getSBKSById(rs.getInt(columnName[i]));
                sbks.next();
                map.put(columnName[i],sbks.getString("departmentmark"));
            }else if(columnName[i].equals("szdlhsdw")){
                // 查所属独立核算单位\
                StringBuffer dlhsdwStr = new StringBuffer("");
                RecordSet dlhsdw = ProjectDetailsUtil.getSBDWById(rs.getString(columnName[i]));
                while (dlhsdw.next()){
                    dlhsdwStr.append(dlhsdw.getString("SUBCOMPANYNAME")+"  ");
                }
                map.put(columnName[i],dlhsdwStr);
            } else if(columnType[i] == 3){
                map.put(columnName[i],FormatUtil.DmFormat(rs.getDouble(columnName[i])));
            }else{
                map.put(columnName[i],rs.getString(columnName[i]));
            }
        }
        List<String>[] lists = new ArrayList[5];
        int[] a = new int[2];
        a[0] = 1;
        a[1] = 2;
        return map;
    }

}
